<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加用户</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
</head>

<!-- 作者：黄佳
添加用户管理 -->

<body>
    <fieldset class="layui-elem-field layui-field-title">
        <legend>
            <button class="layui-btn layui-btn-sm" id="back">返回</button>
            添加用户
        </legend>
    </fieldset>
    <form class="layui-form" action="">

        <div class="layui-form-item">
            <label class="layui-form-label">输入框</label>
            <div class="layui-input-block">
                <input type="text" style="width:190px;" name="mobile" required lay-verify="required" placeholder="请输入用户名" autocomplete="off"
                    class="layui-input" id="txt_phone">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码框</label>
            <div class="layui-input-inline">
                <input type="password" name="pwd" required lay-verify="required" placeholder="请输入密码" autocomplete="off"
                    class="layui-input" id="txt_pwd">
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">管理员</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="isAdmin" lay-skin="switch" lay-text="开启|关闭" id="Admin">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
    </form>

    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.12.4.min.js"></script>
    <script src="../../js/lodash.min.js"></script>

    <script>
        //Demo
        layui.use('form', function () {
            var form = layui.form;

            //监听提交
            form.on('submit(formDemo)', function (data) {
                let admin = false;
                console.log(data.field.isAdmin)
                if (data.field.isAdmin == "on") {
                    admin = true;
                }
                $.ajax({
                    url: "/api/User/",
                    method: "post",
                    data: {
                        mobile: $("#txt_phone").val(),
                        pwd: $("#txt_pwd").val(),
                        isAdmin: admin
                    },
                    success: function () {
                        location.href = "./index.html"
                    },
                    error: function () {

                    }
                })
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });
        });

        //返回
        $("#back").click(function () {
            location.href = "./index.html";
        })
    </script>
</body>

</html>